package com.zhuhanpeng.cloud.spring.control;

import com.alibaba.fastjson.JSON;
import org.springframework.cglib.core.Local;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class BaseController {
    @Resource
    private MessageSource messageSource;

    public String sucessJson(String i18nKey, String... args) {
        Locale local = LocaleContextHolder.getLocale();
        String msg = messageSource.getMessage(i18nKey, args, local);
        Map<String, Object> rst = new HashMap<>();
        rst.put("success", true);
        rst.put("msg", msg);
//        rst.put("code",200);
        return JSON.toJSONString(rst);

    }

    public String totalJson(Integer total, String... args) {
        Map<String, Object> rst = new HashMap<>();
        rst.put("success", true);
        rst.put("msg", total);
        return JSON.toJSONString(rst);
    }

    public String failCodeJson(String i18nKey, Integer code, String... args) {
        Locale local = LocaleContextHolder.getLocale();
        String msg = messageSource.getMessage(i18nKey, args, local);
        Map<String, Object> rst = new HashMap<>();
        rst.put("success", false);
        rst.put("msg", msg);
        rst.put("code", code);
        return JSON.toJSONString(rst); //以JSON的数据格式进行输出，json字符串的形式，

    }

    public String successObjJson(String i18nKey, Object obj, String... args) {
        Locale local = LocaleContextHolder.getLocale();
        String msg = messageSource.getMessage(i18nKey, args, local);
        Map<String, Object> rst = new HashMap<>();
        rst.put("success", true);
        rst.put("msg", msg);
        rst.put("obj", obj);
        return JSON.toJSONString(rst);
    }

    public String successData(Object data) {
        Map<String, Object> rst = new HashMap<>();
        rst.put("success", true);
        rst.put("data", data);
        return JSON.toJSONString(rst);
    }

}